<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/17
 * Time: 15:09
 */
require 'config.php';
switch (@$_GET['a']){
    case 'exit':
        $time = date('Y-m-d H:i:s');
        $uname = $_COOKIE['uname'];
        $sql ="update user set lastlogin = '{$time}' where userName = '{$uname}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            setcookie('uname','',time()-1,'/');
            setcookie('auth','',time()-1,'/');
            echo "<script>alert('退出成功');window.location.href='../index.php';</script>";die;
        }
        mysqli_close($link);
        break;
    case 'jia':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $sql = "update post set elite = 1 where title = '{$title}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('加精成功');window.location.href='post.php?p={$p}';
</script>";die;
        }else{
            echo "<script>alert('加精失败');window.location.href='post.php?p={$p}';
</script>";die;
        }
        mysqli_close($link);
        break;
    case 'fen_jia':
        $title = $_GET['title'];
        $search = $_GET['search'];
        $p = $_GET['p'];
        $sql = "update post set elite = 1 where title = '{$title}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('加精成功');window.location.href='post.php?p={$p}&search={$search}';
</script>";die;
        }else{
            echo "<script>alert('加精失败');window.location.href='post.php?p={$p}&search={$search}';
</script>";die;
        }
        mysqli_close($link);
        break;
    case 'jia_del':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $sql = "update post set elite = 0 where title = '{$title}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('取消加精成功');window.location.href='post.php?p={$p}';
</script>";die;
        }else{
            echo "<script>alert('取消加精失败');window.location.href='post.php?p={$p}';
</script>";die;
        }
        mysqli_close($link);
        break;
    case 'fen_jdel':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $search = $_GET['search'];
        $sql = "update post set elite = 0 where title = '{$title}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('取消加精成功');window.location.href='post.php?p={$p}&search={$search}';
</script>";die;
        }else{
            echo "<script>alert('取消加精失败');window.location.href='post.php?p={$p}&search={$search}';
</script>";die;
        }
        mysqli_close($link);
        break;
    case 'zhi':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $sql = "update post set top = 1 where title = '{$title}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('置顶成功');window.location.href='post.php?p={$p}';
</script>";die;
        }else{
            echo "<script>alert('置顶失败');window.location.href='post.php?p={$p}';
</script>";die;
        }
        mysqli_close($link);
        break;
    case 'fen_zhi':
        $title = $_GET['title'];
        $search = $_GET['search'];
        $p = $_GET['p'];
        $sql = "update post set top = 1 where title = '{$title}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('置顶成功');window.location.href='post.php?p={$p}&search={$search}';
</script>";die;
        }else{
            echo "<script>alert('置顶失败');window.location.href='post.php?p={$p}&search={$search}';
</script>";die;
        }
        mysqli_close($link);
        break;
    case 'zhi_del':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $sql = "update post set top = 0 where title = '{$title}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('取消置顶成功');window.location.href='post.php?p={$p}';
</script>";die;
        }else{
            echo "<script>alert('取消置顶失败');window.location.href='post.php?p={$p}';
</script>";die;
        }
        mysqli_close($link);
        break;
    case  'fen_zdel':
        $title = $_GET['title'];
        $search = $_GET['search'];
        $p = $_GET['p'];
        $sql = "update post set top = 0 where title = '{$title}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('取消置顶成功');window.location.href='post.php?p={$p}&search={$search}';
</script>";die;
        }else{
            echo "<script>alert('取消置顶失败');window.location.href='post.php?p={$p}&search={$search}';
</script>";die;
        }
        mysqli_close($link);
        break;
    case 'search':
        if(!empty($_POST['title'])){
            $title = $_POST['title'];
            $sql = "select * from post where post.recycle = 0 and title like '%{$title}%'";
            $result = mysqli_query($link,$sql);
            $rows = mysqli_fetch_assoc($result);
            if($rows){
                echo "<script>window.location.href='post.php?search={$title}'</script>";die;
            }else{
                echo "<script>alert('帖子不存在');window.location.href='post.php?search=';</script>";die;
            }

        }else{
            echo "<script>window.location.href='post.php?search='</script>";die;
        }
        mysqli_free_result($result);
        mysqli_close($link);
        break;
    case 'delete':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $sql = "update post set recycle = 1 where title = '$title'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)){
            echo "<script>alert('回收成功');window.location.href='post.php?p=$p';</script>";die;
        }else{
            echo "<script>alert('回收失败');window.location.href='.post.php?p=$p';</script>";die;
        }
        mysqli_close($link);
        break;
    case 'del_search':
        if(!empty($_POST['title'])){
            $title = $_POST['title'];
            $sql = "select * from post where post.recycle = 1 and  title like '%{$title}%'";
            $result = mysqli_query($link,$sql);
            $rows = mysqli_fetch_assoc($result);
            if($rows){
                echo "<script>window.location.href='delete.php?search={$title}'</script>";die;
            }else{
                echo "<script>alert('帖子不存在');window.location.href='delete.php?search=';</script>";die;
            }

        }else{
            echo "<script>window.location.href='delete.php?search='</script>";die;
        }
        mysqli_close($link);
        break;
    case 'reply':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $sql = "update post set recycle = 0 where title = '$title'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)){
            echo "<script>alert('恢复成功');window.location.href='delete.php?p=$p';</script>";die;
        }else{
            echo "<script>alert('恢复失败');window.location.href='.delete.php?p=$p';</script>";die;
        }
        mysqli_close($link);
        break;
    case 'del':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $sql = "delete from post where title = '$title'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)){
            echo "<script>alert('删除成功');window.location.href='delete.php?p=$p';</script>";die;
        }else{
            echo "<script>alert('删除失败');window.location.href='.delete.php?p=$p';</script>";die;
        }
        mysqli_close($link);
        break;
    case 'del_rel':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $name = $_GET['search'];
        $id =$_GET['id'];
        $pid = $_GET['pid'];
        $uid = $_GET['uid'];
        $sql = "delete from reply where id = '$id'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)){
            echo "<script>alert('删除成功');window.location.href='replay.php?search={$name}&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }else{
            echo "<script>alert('删除失败');window.location.href='replay.php?search={$name}&p=$p&pid=$pid&uid=$uid&title=$title';</script>";die;
        }
        mysqli_close($link);
        break;
    case 'rel_search':
        if(!empty($_POST['title'])){
            $utitle = $_POST['utitle'];
            $title = $_POST['title'];
            $pid = $_POST['pid'];
            $uid = $_POST['uid'];
            $sql = "select * from reply where content like '%{$utitle}%'";
            $result = mysqli_query($link,$sql);
            $rows = mysqli_fetch_assoc($result);
            if($rows){
                echo "<script>window.location.href='replay.php?search={$utitle}&pid={$pid}&uid={$uid}&title={$title}'</script>";die;
            }else{
                echo "<script>alert('帖子不存在');window.location.href='replay.php?search=&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
            }

        }else{
            echo "<script>window.location.href='delete.php?search='</script>";die;
        }
        mysqli_close($link);

        break;
    case 'jyan':
        $title = $_GET['title'];
        $p = $_GET['p'];
        $pid = $_GET['pid'];
        $id = $_GET['id'];
        $uid = $_GET['uid'];
        $content = $_GET['content'];
        $sql = "update reply set status = 0 where id = '{$id}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('禁用成功');window.location.href='replay.php?search=&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }else{
            echo "<script>alert('禁用失败');window.location.href='replay.php?search=&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }
        break;
    case 'fang':

        $p = $_GET['p'];
        $title = $_GET['title'];
        $pid = $_GET['pid'];
        $uid = $_GET['uid'];
        $id = $_GET['id'];
        $content = $_GET['content'];
        $sql = "update reply set status = 1 where id = '$id'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('开放成功');window.location.href='replay.php?search=&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }else{
            echo "<script>alert('开放失败');window.location.href='replay.php?search=&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }
        break;
    case 'fen_jyan':
        $title = $_GET['title'];
        $id = $_GET['id'];
        $name = $_GET['search'];
        $p = $_GET['p'];
        $pid = $_GET['pid'];
        $uid = $_GET['uid'];
        $content = $_GET['content'];
        $sql = "update reply set status = 0 where id = '$id'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('禁用成功');window.location.href='replay.php?search=$name&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }else{
            echo "<script>alert('禁用失败');window.location.href='replay.php?search=$name&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }
        break;
    case 'fen_fang':
        $name = $_GET['search'];
        $id = $_GET['id'];
        $p = $_GET['p'];
        $title = $_GET['title'];
        $pid = $_GET['pid'];
        $uid = $_GET['uid'];
        $content = $_GET['content'];
        $sql = "update reply set status = 1 where id = '$id'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            echo "<script>alert('开放成功');window.location.href='replay.php?search=$name&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }else{
            echo "<script>alert('开放失败');window.location.href='replay.php?search=$name&p={$p}&pid={$pid}&uid={$uid}&title={$title}';</script>";die;
        }
        break;


}